<?php
namespace BP\Frm;
require_once("BP/Tools/DateUtils.php");
require_once("BP/Tools/StringHelper.php");
require_once("java/util/Map/Entry.php");
class Dev2Interface {
	public static function CreateBlankBillID ($frmID, $userNo, $htParas) // [String frmID, String userNo, Hashtable htParas]
	{
		$gb = GenerBill::constructor__();
		$i = $gb->Retrieve(GenerBillAttr::$FrmID, $frmID, GenerBillAttr::$Starter, $userNo, GenerBillAttr::$BillState, 0);
		if (($i == 1))
		{
			return $gb->getWorkID();
		}
		$fb = FrmBill::constructor__String($frmID);
		$gb->setWorkID(DBAccess->GenerOID("WorkID"));
		$gb->setBillState(BillState::$None);
		$gb->setStarter($WebUser->getNo());
		$gb->setStarterName($WebUser->getName());
		$gb->setFrmName($fb->getName());
		$gb->setFrmID($fb->getNo());
		$gb->setFK_FrmTree($fb->getFK_FormTree());
		$gb->setRDT(DataType->getCurrentDataTime());
		$gb->setNDStep(1);
		$gb->setNDStepName("启动");
		$rpt = new GERpt($frmID);
		if (($fb->getEntityType() == EntityType::$FrmBill))
		{
			$gb->setTitle(Dev2Interface::GenerTitle($fb->getTitleRole(), $rpt));
			$gb->setBillNo($BP->Frm->Dev2Interface->GenerBillNo($fb->getBillNoFormat(), $gb->getWorkID(), NULL, $frmID));
		}
		if ((($fb->getEntityType() == EntityType::$EntityTree) || ($fb->getEntityType() == EntityType::$FrmDict)))
		{
			$rpt->getEnMap()->setCodeStruct($fb->getEnMap()->getCodeStruct());
			$gb->setBillNo($rpt->GenerNewNoByKey("BillNo"));
			$gb->setTitle("");
		}
		$gb->DirectInsert();
		$rpt->SetValByKey("BillState", $gb->getBillState()->getValue());
		$rpt->SetValByKey("Starter", $gb->getStarter());
		$rpt->SetValByKey("StarterName", $gb->getStarterName());
		$rpt->SetValByKey("RDT", $gb->getRDT());
		$rpt->SetValByKey("Title", $gb->getTitle());
		$rpt->SetValByKey("BillNo", $gb->getBillNo());
		$rpt->setOID($gb->getWorkID());
		$rpt->InsertAsOID($gb->getWorkID());
		return $gb->getWorkID();
	}
	public static function CreateBlankDictID ($frmID, $userNo, $htParas) // [String frmID, String userNo, Hashtable htParas]
	{
		$fb = FrmBill::constructor__String($frmID);
		$rpt = new GERpt($frmID);
		$i = $rpt->Retrieve("Starter", $WebUser->getNo(), "BillState", 0);
		if (($i >= 1))
		{
			$rpt->SetValByKey("RDT", $DataType->getCurrentDate());
			$rpt->Update();
			return $rpt->getOID();
		}
		$rpt->SetValByKey("BillState", 0);
		$rpt->SetValByKey("Starter", $WebUser->getNo());
		$rpt->SetValByKey("StarterName", $WebUser->getName());
		$rpt->SetValByKey("RDT", $DataType->getCurrentDate());
		$rpt->getEnMap()->setCodeStruct($fb->getEnMap()->getCodeStruct());
		$rpt->SetValByKey("BillNo", $rpt->GenerNewNoByKey("BillNo"));
		$rpt->setOID(DBAccess->GenerOID($frmID));
		$rpt->InsertAsOID($rpt->getOID());
		return $rpt->getOID();
	}
	public static function SaveWork ($frmID, $workID) // [String frmID, long workID]
	{
		$fb = FrmBill::constructor__String($frmID);
		$gb = GenerBill::constructor__();
		$gb->setWorkID($workID);
		$i = $gb->RetrieveFromDBSources();
		if (($i == 0))
		{
			return "";
		}
		$gb->setBillState(BillState::$Editing);
		$rpt = new GERpt($gb->getFrmID(), $workID);
		if ((($fb->getEntityType() == EntityType::$EntityTree) || ($fb->getEntityType() == EntityType::$FrmDict)))
		{
			$gb->setTitle($rpt->getTitle());
			$gb->Update();
			return "保存成功...";
		}
		if ((($DataType->IsNullOrEmpty($gb->getBillNo()) ==  TRUE ) && !((($fb->getEntityType() == EntityType::$EntityTree) || ($fb->getEntityType() == EntityType::$FrmDict)))))
		{
			$gb->setBillNo($BP->Frm->Dev2Interface->GenerBillNo($fb->getBillNoFormat(), $workID, NULL, $fb->getPTable()));
			if ((DBAccess->IsExitsTableCol($fb->getPTable(), "BillNo") ==  TRUE ))
			{
				DBAccess::RunSQL(((((("UPDATE " . $fb->getPTable()) . " SET BillNo='") . $gb->getBillNo()) . "' WHERE OID=") . $workID));
			}
		}
		if ((($DataType->IsNullOrEmpty($gb->getTitle()) ==  TRUE ) && !((($fb->getEntityType() == EntityType::$EntityTree) || ($fb->getEntityType() == EntityType::$FrmDict)))))
		{
			$gb->setTitle(Dev2Interface::GenerTitle($fb->getTitleRole(), $rpt));
			if ((DBAccess->IsExitsTableCol($fb->getPTable(), "Title") ==  TRUE ))
			{
				DBAccess::RunSQL(((((("UPDATE " . $fb->getPTable()) . " SET Title='") . $gb->getTitle()) . "' WHERE OID=") . $workID));
			}
		}
		$gb->Update();
		$rpt->setTitle($gb->getTitle());
		$rpt->setBillNo($gb->getBillNo());
		$rpt->Update();
		return "保存成功...";
	}
	public static function SaveAsDraft ($frmID, $workID) // [String frmID, long workID]
	{
		$gb = GenerBill::constructor__L($workID);
		if (($gb->getBillState() != BillState::$None))
		{
			return "err@只有在None的模式下才能保存草稿�?";
		}
		if (($gb->getBillState() != BillState::$Editing))
		{
			$gb->setBillState(BillState::$Editing);
			$gb->Update();
		}
		return "保存成功...";
	}
	public static function MyBill_Delete ($frmID, $workID) // [String frmID, long workID]
	{
		$fb = FrmBill::constructor__String($frmID);
		$sqls = ("DELETE FROM Frm_GenerBill WHERE WorkID=" . $workID);
		$sqls += ((("@DELETE FROM " . $fb->getPTable()) . " WHERE OID=") . $workID);
		DBAccess->RunSQLs($sqls);
		return "删除成功.";
	}
	public static function MyDict_Delete ($frmID, $workID) // [String frmID, long workID]
	{
		$fb = FrmBill::constructor__String($frmID);
		$sql = ((("@DELETE FROM " . $fb->getPTable()) . " WHERE OID=") . $workID);
		DBAccess->RunSQLs($sql);
		return "删除成功.";
	}
	public static function MyBill_DeleteDicts ($frmID, $workIds) // [String frmID, String workIds]
	{
		$fb = FrmBill::constructor__String($frmID);
		$sql = (((("DELETE FROM " . $fb->getPTable()) . " WHERE OID in (") . $workIds) . ")");
		DBAccess->RunSQLs($sql);
		return "删除成功.";
	}
	public static function MyEntityTree_Delete ($frmID, $billNo) // [String frmID, String billNo]
	{
		$fb = FrmBill::constructor__String($frmID);
		$sql = (((((("DELETE FROM " . $fb->getPTable()) . " WHERE BillNo='") . $billNo) . "' OR ParentNo='") . $billNo) . "'");
		DBAccess->RunSQLs($sql);
		return "删除成功.";
	}
	public static function MyBill_Copy ($frmID, $workID) // [String frmID, long workID]
	{
		$fb = FrmBill::constructor__String($frmID);
		$gb = GenerBill::constructor__();
		$gb->setWorkID(DBAccess->GenerOID("WorkID"));
		$gb->setBillState(BillState::$None);
		$gb->setStarter($WebUser->getNo());
		$gb->setStarterName($WebUser->getName());
		$gb->setFrmName($fb->getName());
		$gb->setFrmID($fb->getNo());
		$gb->setFK_FrmTree($fb->getFK_FormTree());
		$gb->setRDT(DataType->getCurrentDataTime());
		$gb->setNDStep(1);
		$gb->setNDStepName("启动");
		$rpt = new GERpt($frmID, $workID);
		$gb->setTitle(Dev2Interface::GenerTitle($fb->getTitleRole(), $rpt));
		$gb->setBillNo($BP->Frm->Dev2Interface->GenerBillNo($fb->getBillNoFormat(), $gb->getWorkID(), NULL, $frmID));
		$gb->DirectInsert();
		$rpt->SetValByKey("BillState", $gb->getBillState()->getValue());
		$rpt->SetValByKey("Starter", $gb->getStarter());
		$rpt->SetValByKey("StarterName", $gb->getStarterName());
		$rpt->SetValByKey("RDT", $gb->getRDT());
		$rpt->SetValByKey("Title", $gb->getTitle());
		$rpt->SetValByKey("BillNo", $gb->getBillNo());
		$rpt->setOID($gb->getWorkID());
		$rpt->InsertAsOID($gb->getWorkID());
		$dtls = new MapDtls($frmID);
		if (($dtls->size() > 0))
		{
			foreach ($dtls->ToJavaList() as $dtl) 			{
				if (($dtl->getIsCopyNDData() ==  FALSE ))
				{
					continue;
				}
				$dtlData = new GEDtl($dtl->getNo());
				$dtlsFromData = new GEDtls($dtl->getNo());
				$dtlsFromData->Retrieve($GEDtlAttr->RefPK, $workID);
				foreach ($dtlsFromData->ToJavaList() as $geDtlFromData) 				{
					$dbs = NULL;
					if (($dtl->getIsEnableAthM() ==  TRUE ))
					{
						$dbs = new FrmAttachmentDBs();
						$dbs->Retrieve($FrmAttachmentDBAttr->RefPKVal, $geDtlFromData->getOID());
					}
					$dtlData->Copy($geDtlFromData);
					$dtlData->setRefPK($String->valueOf($rpt->getOID()));
					$dtlData->InsertAsNew();
					if ((($dbs != NULL) && ($dbs->size() != 0)))
					{
						$newDB = new FrmAttachmentDB();
						foreach ($dbs->ToJavaList() as $db) 						{
							$newDB->Copy($db);
							$newDB->setRefPKVal($String->valueOf($dtlData->getOID()));
							$newDB->setFID($dtlData->getOID());
							$newDB->setMyPK(DBAccess->GenerGUID());
							$newDB->Insert();
						}
					}
				}
			}
		}
		$athDecs = new FrmAttachments($frmID);
		if (($athDecs->size() > 0))
		{
			foreach ($athDecs->ToJavaList() as $athDec) 			{
				$aths = new FrmAttachmentDBs();
				$aths->Retrieve($FrmAttachmentDBAttr->FK_FrmAttachment, $athDec->getMyPK(), $FrmAttachmentDBAttr->RefPKVal, $workID);
				foreach ($aths->ToJavaList() as $athDB) 				{
					$athDB_N = new FrmAttachmentDB();
					$athDB_N->Copy($athDB);
					$athDB_N->setRefPKVal($String->valueOf($rpt->getOID()));
					$athDB_N->setMyPK(DBAccess->GenerGUID());
					$athDB_N->Insert();
				}
			}
		}
		return "复制成功.";
	}
	public static function DB_StartBills ($empID) // [String empID]
	{
		$ds = new DataSet();
		$ens = new FrmTrees();
		$ens->RetrieveAll();
		$dtSort = $ens->ToDataTableField("Sort");
		$dtSort->TableName = "Sort";
		$ds->Tables->add($dtSort);
		$bills = FrmBills::constructor__();
		$bills->RetrieveAll();
		$dtStart = $bills->ToDataTableField();
		$dtStart->TableName = "Start";
		$ds->Tables->add($dtStart);
		return $ds;
	}
	public static function DB_Todolist ($empID) // [String empID]
	{
		return new DataTable();
	}
	public static function DB_Draft ($frmID, $empID) // [String frmID, String empID]
	{
		if (($DataType->IsNullOrEmpty($empID) ==  TRUE ))
		{
			$empID = $WebUser->getNo();
		}
		$bills = GenerBills::constructor__();
		$bills->Retrieve(GenerBillAttr::$FrmID, $frmID, GenerBillAttr::$Starter, $empID);
		return $bills->ToDataTableField();
	}
	public static function GenerTitle ($titleRole, $wk) // [String titleRole, Entity wk]
	{
		if ($DataType->IsNullOrEmpty($titleRole))
		{
			$myattr = $wk->getEnMap()->getAttrs()->GetAttrByKey("Title");
			if (($myattr == NULL))
			{
				$myattr = $wk->getEnMap()->getAttrs()->GetAttrByKey("Title");
			}
			if (($myattr != NULL))
			{
				$titleRole = $myattr->getDefaultVal();
			}
			if (($DataType->IsNullOrEmpty($titleRole) || ($titleRole->contains("@") ==  FALSE )))
			{
				$titleRole = "@WebUser.getFK_DeptName-@WebUser.getNo(),@WebUser.getName()在@RDT发起.";
			}
		}
		if (($titleRole->equals("@OutPara") || ($DataType->IsNullOrEmpty($titleRole) ==  TRUE )))
		{
			$titleRole = "@WebUser.getFK_DeptName-@WebUser.getNo(),@WebUser.getName()在@RDT发起.";
		}
		$titleRole = $titleRole->replace("@WebUser.getNo()", $WebUser->getNo());
		$titleRole = $titleRole->replace("@WebUser.getName()", $WebUser->getName());
		$titleRole = $titleRole->replace("@WebUser.getFK_DeptNameOfFull", $WebUser->getFK_DeptNameOfFull());
		$titleRole = $titleRole->replace("@WebUser.getFK_DeptName", $WebUser->getFK_DeptName());
		$titleRole = $titleRole->replace("@WebUser.getFK_Dept()", $WebUser->getFK_Dept());
		$titleRole = $titleRole->replace("@RDT", $DateUtils->format(new Date(), "yy年MM月dd日HH时mm�?"));
		if ($titleRole->contains("@"))
		{
			$attrs = $wk->getEnMap()->getAttrs();
			foreach ($attrs as $attr) 			{
				if (($titleRole->contains("@") ==  FALSE ))
				{
					break;
				}
				if (($attr->getIsRefAttr() ==  FALSE ))
				{
					continue;
				}
				$titleRole = $titleRole->replace(("@" . $attr->getKey()), $wk->GetValStrByKey($attr->getKey()));
			}
			foreach ($attrs as $attr) 			{
				if (($titleRole->contains("@") ==  FALSE ))
				{
					break;
				}
				if (($attr->getIsRefAttr() ==  TRUE ))
				{
					continue;
				}
				$titleRole = $titleRole->replace(("@" . $attr->getKey()), $wk->GetValStrByKey($attr->getKey()));
			}
		}
		$titleRole = $titleRole->replace('~', '-');
		$titleRole = $titleRole->replace("'", "�?");
		$wk->SetValByKey("Title", $titleRole);
		return $titleRole;
	}
	public static function GenerBillNo ($billNo, $workid, $en, $frmID) // [String billNo, long workid, Entity en, String frmID]
	{
		if ($DataType->IsNullOrEmpty($billNo))
		{
			$billNo = "3";
		}
		if ($billNo->contains("@"))
		{
			$billNo = $BP->WF->Glo->DealExp($billNo, $en, NULL);
		}
		$billNo = $billNo->replace("{YYYY}", $DateUtils->format(new Date(), "yyyy"));
		$billNo = $billNo->replace("{yyyy}", $DateUtils->format(new Date(), "yyyy"));
		$billNo = $billNo->replace("{yy}", $DateUtils->format(new Date(), "yy"));
		$billNo = $billNo->replace("{YY}", $DateUtils->format(new Date(), "yy"));
		$billNo = $billNo->replace("{MM}", $DateUtils->format(new Date(), "MM"));
		$billNo = $billNo->replace("{mm}", $DateUtils->format(new Date(), "MM"));
		$billNo = $billNo->replace("{DD}", $DateUtils->format(new Date(), "dd"));
		$billNo = $billNo->replace("{dd}", $DateUtils->format(new Date(), "dd"));
		$billNo = $billNo->replace("{HH}", $DateUtils->format(new Date(), "HH"));
		$billNo = $billNo->replace("{hh}", $DateUtils->format(new Date(), "HH"));
		$billNo = $billNo->replace("{LSH}", $String->valueOf($workid));
		$billNo = $billNo->replace("{WorkID}", $String->valueOf($workid));
		$billNo = $billNo->replace("{OID}", $String->valueOf($workid));
		if ($billNo->contains("@WebUser.DeptZi"))
		{
			$val = DBAccess->RunSQLReturnStringIsNull((("SELECT Zi FROM Port_Dept WHERE No='" . $WebUser->getFK_Dept()) . "'"), "");
			$billNo = $billNo->replace("@WebUser.DeptZi", $val);
		}
		$sql = "";
		$num = 0;
		$supposeBillNo = $billNo;
		$loc = [];// 
		$lsh = null;
		$lshIdx = -1;
		$map = []; //
		for ($i = 2; ($i < 9); ++$i) 
		{
			$lsh = (("{LSH" . $i) . "}");
			if (!$supposeBillNo->contains($lsh))
			{
				continue;
			}
			while ($supposeBillNo->contains($lsh)) 
			{
				$lshIdx = $supposeBillNo->indexOf($lsh);
				$supposeBillNo = (((( (($lshIdx == 0)) ? "" : $supposeBillNo->substring(0, $lshIdx) )) + $StringHelper->padLeft("", $i, '_')) + (( ((($lshIdx + 6) < $supposeBillNo->length())) ? $supposeBillNo->substring(($lshIdx + 6)) : "" )));
				$map->put($lshIdx, $i);
			}
		}
		$iterator = $map->entrySet()->iterator();
		while ($iterator->hasNext()) 
		{
			$entry = $iterator->next();
			$loc->add($entry);
		}
		$sql = (((((((("SELECT BillNo FROM Frm_GenerBill WHERE BillNo LIKE '" . $supposeBillNo->replace("[", "[[]")) . "'") . " AND WorkID <> ") . $workid) . " AND FrmID ='") . $frmID) . "' ") . " ORDER BY BillNo DESC ");
		$maxBillNo = DBAccess->RunSQLReturnString($sql);
		$ilsh = 0;
		if ($DataType->IsNullOrEmpty($maxBillNo))
		{
			foreach ($loc as $kv) 			{
				$supposeBillNo = (((( (($kv->getKey() == 0)) ? "" : $supposeBillNo->substring(0, $kv->getKey()) )) + $StringHelper->padLeft("1", $kv->getValue(), '0')) + (( (((($kv->getKey() + $kv->getValue()) + 1) < $supposeBillNo->length())) ? $supposeBillNo->substring(($kv->getKey() + $kv->getValue())) : "" )));
			}
		}
		else
		{
			$mlsh = []; //
			$plus1idx = -1;
			for ($i = ($loc->size() - 1); ($i >= 0); --$i) 
			{
				$ilsh = $Integer->parseInt($StringHelper->substring($maxBillNo, $loc->get($i)->getKey(), $loc->get($i)->getValue()));
				if (($plus1idx >= 0))
				{
					++$ilsh;
					$plus1idx = -1;
				}
				else
				{
					$mlsh->put($loc->get($i)->getKey(), ( (($i == ($loc->size() - 1))) ? ($ilsh + 1) : $ilsh ));
					continue;
				}
				if (($ilsh >= $Integer->parseInt($StringHelper->padLeft("", $loc->get($i)->getValue(), '9'))))
				{
					if (($i > 0))
					{
						$mlsh->put($loc->get($i)->getKey(), 1);
					}
					else
					{
						$supposeBillNo = "单据号超出范�?";
						break;
					}
					$plus1idx = ($i - 1);
				}
				else
				{
					$mlsh->put($loc->get($i)->getKey(), ($ilsh + 1));
				}
			}
			if ($supposeBillNo->equals("单据号超出范�?"))
			{
				return $supposeBillNo;
			}
			foreach ($loc as $kv) 			{
				$supposeBillNo = (((( (($kv->getKey() == 0)) ? "" : $supposeBillNo->substring(0, $kv->getKey()) )) + $StringHelper->padLeft($mlsh->get($kv->getKey()), $kv->getValue(), '0')) + (( (((($kv->getKey() + $kv->getValue()) + 1) < $supposeBillNo->length())) ? $supposeBillNo->substring(($kv->getKey() + $kv->getValue())) : "" )));
			}
		}
		$billNo = $supposeBillNo;
		return $billNo;
	}
}
?>
